<template>
  <!--登录表单的容器-->
  <div class="login_container">
    <!--登录区域-->
    <div class="login_box">
      <!--头像-->
      <div class="avatar_box">
        <img src="../assets/33.png">
      </div>
      <!--表单-->
      <el-form :model="loginForm" :rules="loginRules" ref="loginForm" label-width="0px" class="login_form">
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" placeholder="请输入用户名称" prefix-icon="el-icon-user-solid"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" placeholder="请输入登录密码" prefix-icon="el-icon-lock"></el-input>
        </el-form-item>
        <el-form-item prop="phonse">
          <el-input v-model="loginForm.phonse" placeholder="请输入手机号码" prefix-icon="el-icon-lock"></el-input>
        </el-form-item>
        <el-form-item prop="verifyCode">
          <div class="verifyCode_box">
            <el-input v-model="loginForm.verifyCode" placeholder="请输入手机验证码" prefix-icon="el-icon-mobile" class="verifyCode"></el-input>
            <el-button type="primary" @click="getSend()" icon="el-icon-success" style="position: relative;left: 5px;">获取验证码</el-button>
          </div>
        </el-form-item>
        <el-form-item class="login_btn">
          <el-button type="primary" @click="submitForm('loginForm')">立即创建</el-button>
          <el-button @click="resetForm('loginForm')">重置</el-button>
        </el-form-item>
      </el-form>
      <!-- 拼图验证码 -->
      <div v-show="flag" class="testCode">
        <!-- <p class="closeBtn"><i class="el-icon-close" @click="flag=!flag"></i></p>-->
        <slide-verify
          :l="42"
          :r="20"
          :w="362"
          :h="140"
          slider-text="向右滑动"
          @success="onSuccess"
          @fail="onFail"
          @refresh="onRefresh"
          :style="{width:'362px'}"
          class="slide-box"
          ref="slideBlock"
          v-show="flag"
        ></slide-verify>
      </div>
    </div>

  </div>
</template>

<script>
  import {getSendSms} from "../api/user"

  export default {
    name: 'Login',
    data() {
      var validateCode = (rule, value, callback) => {
        var time=new Date();
        var date=new Date(this.startdate);
        /*if (value === '') {
          callback(new Error('请输入验证码'));
        } else if (this.yzm === null) {
          callback(new Error('请获取验证码!'));
        }else if(time>date){
          callback(new Error('验证码已失效!'));
        } else if (parseInt(value)!== this.yzm) {
          callback(new Error('验证码错误!'));
        }else{
          callback();
        }*/
      };

      return {
        loginForm: {
          username: '',
          password: '',
          verifyCode: '',
          phonse: '',
        },
        loginRules: {
          username: [
            { required: true, message: '请输入用户名称', trigger: 'blur' },
            { min: 3, max: 16, message: '长度在 3 到 16 个字符', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '请输入登录密码', trigger: 'blur' },
            { min: 3, max: 16, message: '长度在 3 到 16 个字符', trigger: 'blur' }
          ],
          verifyCode: [
            { required: true,message: '请输入验证码', trigger: 'blur' }  /*validator: validateCode*/
          ],
          phonse: [
            { required: true, message: '请输入手机号码', trigger: 'blur' },
            { min: 11, max: 11, message: '请输入手正确的手机号码', trigger: 'blur' }
          ]
        },
        msg: '',
        flag: false,
        message:'',
        yzm: null,
        startdate: null,
      };
    },
    methods: {
      // 拼图成功
      onSuccess (){
        this.flag=false;
        this.getLogin()
      },
      // 拼图失败
      onFail (){
        this.msg = ''
      },
      // 拼图刷新
      onRefresh (){
        this.msg = ''
      },
      // 登录请求接口
      getLogin () {
        var $this=this;
        this.axios.post("http://localhost:8080/login",null,{
          params:{
            username:$this.loginForm.username,
            password:$this.loginForm.password,
          }
        })
          .then(function (response) {
            console.log(response.data);
            if (response.data.success){
              $this.$router.push("/main");
              window.sessionStorage.setItem("user",JSON.stringify(response.data.data.user));
              window.sessionStorage.setItem("menuList",JSON.stringify(response.data.data.menuList));
              window.localStorage.setItem("accessToken",response.data.data.token);
            }else{
              $this.message=response.data.message;
              $this.open4()
            }

          })
          .catch(function (erro) {
            console.log("获取失败！");
          })

      },
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {

            this.flag = true;
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
      open4() {
        this.$message.error(this.message);
      },
      getSend(){
        var $this=this;
        getSendSms(this.loginForm.phonse)
          .then(function (response) {
            $this.yzm=response.data.data.yzm;
            $this.startdate=response.data.data.time;
          })
          .catch(function (erro) {
            console.log("请求错误！")
          })
      }
    }
  }
</script>

<style lang="less" scoped>
  .login_container {
    height: 100%;
    background-color: white;
  }

  .login_box {
    width: 450px;
    height: 380px;
    background-color: #FFFFFF;
    border-radius: 3px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);

    .avatar_box{
      width: 130px;
      height: 130px;
      border: 1px solid #EEEEEE;
      border-radius: 50%;
      padding: 10px;
      box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
      margin: -65px auto;
      background-color: #FFFFFF;

      img{
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background-color: #EEEEEE;
      }
    }

    .login_form{
      position: absolute;
      bottom: 0px;
      width: 100%;
      padding: 0px 20px;
      box-sizing: border-box;

      .login_btn{
        display: flex;
        justify-content: flex-end;
      }

      .verifyCode_box{
        display: flex;
        .verifyCode{
          width: 70%;
          justify-content: left;
        }

      }
    }

    .testCode{

    }
  }
</style>
